package com.zr.storehouse.service;

import com.zr.storehouse.mapper.StoreMapper;
import com.zr.storehouse.model.ResAttribute;
import com.zr.storehouse.model.ResDetailsUpdateVo;
import com.zr.storehouse.model.ResDetailsVo;
import com.zr.storehouse.model.StorageDetailsVo;
import com.zr.util.AllRecords;
import com.zr.util.ResultVO;
import com.zr.util.ResultVOBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/1/11.
 */
@Service
public class StoreService {
    @Autowired
    private StoreMapper storeMapper;
    public ResultVO<List<ResAttribute>> queryBusinessAttribute() {
    List<ResAttribute> list=storeMapper.findAttr();
    if (list.size()==0){
     return ResultVOBuilder.error("200","库区属性为空");
    }
    return ResultVOBuilder.success(list);
    }

    public ResultVO<AllRecords> queryPage(ResDetailsVo resDetailsVo) {
        System.out.println(resDetailsVo);
        AllRecords allRecords=new AllRecords();
        int count=storeMapper.getRdCount(resDetailsVo);
//        System.out.println(count);
        List<ResDetailsVo> list=storeMapper.queryRdPage(resDetailsVo);
//        System.out.println(list);
        List<StorageDetailsVo> storageDetailsVos=storeMapper.queryStorePage(resDetailsVo.getWareHouseId());
        for (ResDetailsVo detailsVo:list){
            if (detailsVo.getStatus()){
                detailsVo.setStatusName("启用");
            }
            if (!detailsVo.getStatus()){
                detailsVo.setStatusName("禁用");
            }
            for (StorageDetailsVo detailsVo1:storageDetailsVos){
                detailsVo.setWareHouseId(detailsVo1.getId());
                detailsVo.setWareHouseCode(detailsVo1.getCode());
                detailsVo.setWareHouseName(detailsVo1.getName());
                detailsVo.setWareHouseRemark(detailsVo1.getRemark());
            }
        }

        System.out.println(list);
        allRecords.setDataList(list);
        allRecords.setTotalNumber(count);
        allRecords.setPageIndex(resDetailsVo.getPageIndex());
        allRecords.setPageSize(resDetailsVo.getPageSize());
        allRecords.resetTotalNumber(count);
        return ResultVOBuilder.success(allRecords);
    }

    public ResultVO<ResDetailsUpdateVo> Update(ResDetailsUpdateVo resDetailsUpdateVo) {
        List<ResDetailsVo> resDetailsVos=storeMapper.findById(resDetailsUpdateVo.getId());
        if (resDetailsVos.size()<1){
            return ResultVOBuilder.error("100","你要修改的信息不存在");
        }
        List<ResDetailsVo> resDetailsVos1=storeMapper.findByNmae(resDetailsUpdateVo.getName());
        if (resDetailsVos1.size()>0){
            return ResultVOBuilder.error("200","您要修改的信息已存在，请重新输入");
        }
//        BeanUtils.copyProperties(resDetailsUpdateVo,resDetailsVos);
       int i= storeMapper.Update(resDetailsUpdateVo);
       List<ResAttribute> businessAttribute=resDetailsUpdateVo.getBusinessAttribute();
       List<Integer> ids=new ArrayList<>();
       for (ResAttribute ra:businessAttribute){
           ids.add(ra.getBusinessAttributeId());       ;
       }
       List<Integer> atrids=storeMapper.findMiddleId(resDetailsUpdateVo.getId());
        Map map=new HashMap();
        map.put("atrids",atrids);
        map.put("ids",ids);
       storeMapper.updateAttribute(map);
//       storeMapper.InsertAttribute(businessAttribute);
       return ResultVOBuilder.success(resDetailsUpdateVo);
    }

    public ResultVO<List<StorageDetailsVo>> queryWareHouse() {
        List<StorageDetailsVo> list=storeMapper.queryAllStore();
        return ResultVOBuilder.success(list);
    }
@Transactional
    public ResultVO<ResDetailsUpdateVo> AddSection(@Valid ResDetailsUpdateVo resDetailsVo) {
        List<ResDetailsVo> resDetailsVos1=storeMapper.findByCode(resDetailsVo.getCode(),resDetailsVo.getName());
        if (resDetailsVos1.size()>0){
            return ResultVOBuilder.error("200","您要添加的信息已存在，请重新输入");
        }
        if (resDetailsVo.getBusinessAttribute().get(0).getBusinessAttributeId()==null){
            System.out.println(resDetailsVo.getBusinessAttribute());
            return ResultVOBuilder.error("200","不存在Id");
        }
       int id= storeMapper.InsertResDtails(resDetailsVo);
    System.out.println(resDetailsVo.getId());
        List<ResAttribute> businessAttribute=resDetailsVo.getBusinessAttribute();

        List<Integer> ids=new ArrayList<>();
        for (ResAttribute resAttribute:businessAttribute){
            ids.add(resAttribute.getBusinessAttributeId());
        }
        Map map=new HashMap();
        map.put("res_id",resDetailsVo.getId());
        map.put("ids",ids);
        storeMapper.InsertMiddle(map);
        return ResultVOBuilder.success(resDetailsVo);
    }

    public ResultVO<ResDetailsVo> SeeSection(Integer id) {
       List<ResDetailsVo> list= storeMapper.findById(id);
       if (list==null){
           return ResultVOBuilder.error("122","该数据不存在");

       }
       List<ResAttribute> attributes=storeMapper.findMiddle(id);
     for (ResDetailsVo l:list){
    l.setBusinessAttribute(attributes);
          }
          return ResultVOBuilder.success(list.get(0));
    }

    public ResDetailsVo findById(Integer id) {
        return  storeMapper.findById(id).get(0);
    }

    public void updateStatus(ResDetailsVo resDetailsVo) {
        storeMapper.updateStatus(resDetailsVo);
    }
}
